package v3.chapter2.synchronizedBlockLockAll;

public class test3 {

	public static void main(String[] args) {
		// 同一个“对象监视器”
		Service3 service = new Service3();
		MyObject3 object = new MyObject3();

		MyThreadA3 a = new MyThreadA3(service, object);
		a.setName("a");
		a.start();

		MyThreadB3 b = new MyThreadB3(object);
		b.setName("b");
		b.start();

	}

}

class MyThreadA3 extends Thread {
	private Service3 service;
	private MyObject3 object;

	public MyThreadA3(Service3 service, MyObject3 object) {
		super();
		this.service = service;
		this.object = object;
	}

	@Override
	public void run() {
		super.run();
		service.testMethod1(object);
	}
}

class MyThreadB3 extends Thread {
	private MyObject3 object;

	public MyThreadB3(MyObject3 object) {
		super();
		this.object = object;
	}

	@Override
	public void run() {
		super.run();
		object.speedPrintString();
	}
}

class Service3 {
	public void testMethod1(MyObject3 object) {
		synchronized (this) {
			try {
				System.out.println("testMehtod1_getLock time=" + System.currentTimeMillis() + " run ThreadName="
						+ Thread.currentThread().getName());
				Thread.sleep(2000);
				System.out.println("testMehtod1_releaseLock time=" + System.currentTimeMillis() + " run ThreadName="
						+ Thread.currentThread().getName());
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

class MyObject3 {
	synchronized public void speedPrintString() {
		System.out.println("speedPrintString_getLock time=" + System.currentTimeMillis() + " run ThreadName="
				+ Thread.currentThread().getName());
		System.out.println("------------------------------------");
		System.out.println("speedPrintString_releaseLock time=" + System.currentTimeMillis() + " run ThreadName="
				+ Thread.currentThread().getName());
	}
}